<?php

// 初期設定
if (!defined("IN_APP")) {
	define("IN_APP", true);
	include_once("../../include/init.php");
	requireLogin();
}

// ログインしていないユーザーを転送する
if (!LOGGED_IN) {
	$_SESSION["message"] = $MESSAGES["ACCESS_FORBIDDEN"];
	header("Location: {$SETTINGS['SCRIPT_PATH']}");
}

// タブ別の設定の読み込み
require_once("tab.include.php");

// ページ名
$page_name = "group_edit";

/******************* ページの処理 *******************/

// 処理するツイートのidを読み込む
$group_name = trim(@$_GET['subpage']);

//リストにメンバー追加
$connection = connectToDb();

// メンバーのクラス
class Member {
	var $id, $twitter_username, $profile_img, $description, $name, $isLeader, $isLocalName;
}

$group_members = array();
$user_id = $_SESSION['user_id'];
$group_id = $_SESSION['group'];

if (isLeader($connection, $group_id, $user_id)) {

	// 選択されたグループを確認する
	if (isset($_SESSION['group']))
		$selected_group_name = getGroupName($connection, $_SESSION['group']);
	else 
		$selected_group_name = "";
		
	if (!empty($group_name)) {
		if ($group_name != $selected_group_name) {
			$query = sprintf("SELECT group_id FROM groups WHERE title = '%s' AND owner_id = %s ",
						mysql_real_escape_string($group_name),
						mysql_real_escape_string($_SESSION['user_id']) 
					);
			$query.= "AND (del_flag <> 'd' OR del_flag IS NULL)";
			
			$result = mysql_query($query, $connection);
			
			if ( !$result ) {
				exit('Invalid query: $query - ' . mysql_error());
			}
			else {
				$row = mysql_fetch_assoc($result);
				
				$group_id = $row['group_id'];
			}
			
			selectGroup($group_id);
		}
	}

	// データベースのクエリーを準備する
	$query = "SELECT group_members.user_id, member_nickname, name "; 
	$query.= "FROM group_members INNER JOIN users ON group_members.user_id = users.user_id ";
	$query.= sprintf("WHERE group_id = %s ",
				mysql_real_escape_string($_SESSION['group']) );
	$query.= "AND (group_members.del_flag <> 'd' OR group_members.del_flag IS NULL) ";
	$query.= "AND (users.del_flag <> 'd' OR users.del_flag IS NULL) ";
	$query.= "ORDER BY sort_order ASC";
			
	$result = mysql_query($query, $connection);

	$ids = "";

	// メンバー情報を配列にまとめる
	while ($row = mysql_fetch_assoc($result)) {
		$member = new Member();
		
		if (!empty($row['member_nickname'])) {
			$member->name = $row['member_nickname'];
			$member->isLocalName = true;
		}
		else {
			$member->name = $row['name'];
			$member->isLocalName = false;
		}
		$member->id = $row['user_id'];
		
		$member->isLeader = $row['user_id'] == $user_id;
		
		array_push($group_members, $member);
		
		$ids .= $row['user_id'] . ",";
	}

	$ids = substr($ids,0, -1);

	$account = "";

	if (!empty($group_name)) {
		$params = array("user_id" => $ids);

		// ツイッターからユーザー情報を取得する
		$data = $twitter->get_usersLookup($params);
		$list_members = $data->response;
		
		foreach ($list_members as $list_member) {
			foreach ($group_members as $member) {
				if ($member->id == $list_member["id"]) {
					$member->twitter_username = $list_member['screen_name'];
					$member->profile_img = $list_member['profile_image_url'];
					
					if (!empty($list_member['description'])) {
						$description = htmlspecialchars($list_member['description']);
					}
					else {
						$description = "<span style=\"color:gray;\">";
						$description.= $MESSAGES['GROUP_EDIT_NO_DESCRIPTION'];
						$description.= "</span>";
					}
					
					$member->description = $description;
				}
			}
		}
	}
	else {
		$error = $MESSAGES['INVALID_PAGE'];
		showErrorPage();
	}

	$member_list = getGroupOrder($connection, $group_id);

	// 並び替えの関数
	function sortMember($a, $b) {
		global $member_list;
		
		$a_sort = -1;$b_sort = -1;
		
		foreach ($member_list as $key => $value) {
			if ($value == $a->id)
				$a_sort = $key;
			else if ($value == $b->id)
				$b_sort = $key;
		}
				
		if ($a_sort == $b_sort) {
			return 0;
		}
		else {
			return $a_sort < $b_sort ? -1 : 1; 
		}
	}

	// 並び替えを行う
	usort($group_members, "sortMember");
}
else {
	$error = "このページを表示できるのはリーダーのみです。";
	showErrorPage();
}

// データベースの接続を切断する
mysql_close($connection);

/******************** 処理終了 ********************/

// ページを表示する
$include_path = $SETTINGS['VIEW_PATH'] . $tab_name . $page_name . $SETTINGS['VIEW_NAME'];
include($include_path);

?>